package com.shanghaionstar.smart.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.google.gson.Gson;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.smart.ControlSmartDeviceResponse;
import com.onstar.cn.smart.GetSmartDeviceResponse;
import com.onstar.cn.smart.SmartDevicesResponse;
import com.onstar.cn.smart.entity.GeoFencingEntity;
import com.onstar.cn.smart.request.ControlSmartReq;
import com.onstar.cn.smart.request.SaveGeoFencingReq;
import com.onstar.cn.weight.ProgressLayout;
import com.onstar.cn.weight.ToastView;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.smart.service.SmartHomeService;
import com.shanghaionstar.smart.swipemenulistview.BaseSwipeMenuExpandableListAdapter;
import com.shanghaionstar.smart.swipemenulistview.ContentViewWrapper;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.NetConnectUtil;

import java.util.HashMap;
import java.util.List;

import rx.functions.Action1;

public class DeviceExListAdapter extends BaseSwipeMenuExpandableListAdapter implements View.OnClickListener {
    private static final int MIN_TEMP = 17;
    private BaseActivity mContext;
    private List<GetSmartDeviceResponse> deviceList;
    public int curPos = -1;
    private String[] mDistance;
    private SmartDevicesResponse devicesResponse;
    private List<GeoFencingEntity> geoEntitys;
    private ProgressLayout pl;
    private SmartHomeService service;

    public DeviceExListAdapter(BaseActivity context, SmartDevicesResponse devicesResponse, ProgressLayout pl) {
        this.mContext = context;
        this.devicesResponse = devicesResponse;
        this.deviceList = devicesResponse.devices;
        this.geoEntitys = devicesResponse.geofencings;
        mDistance = mContext.getResources().getStringArray(R.array.smart_sel_dis);
        service = new SmartHomeService(mContext);
        this.pl = pl;
    }

    public Object getChild(int groupPosition, int childPosition) {
        GetSmartDeviceResponse device = deviceList.get(groupPosition);
        return device;
    }

    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    private Button lastModeClickBtn;
    private Button lastDistanceClickBtn;

    /**
     * @param iv 控制开关
     */
    private void switchCommandClick(ImageButton iv) {
        GetSmartDeviceResponse item = deviceList.get(curPos);
        if (item.switchStatus == 1) {
            BusinessUtil.sendReportByClickType(mContext, FunctionNameFindId.Smarthome_appdetail_gofenceoff.getStringValue());
            remoteControl(null, -1, -1, 0, iv);
        } else {
            BusinessUtil.sendReportByClickType(mContext, FunctionNameFindId.Smarthome_appdetail_gofenceon.getStringValue());
            remoteControl(null, -1, -1, 1, iv);
        }
    }

    /**
     * 在子线程中关闭进度条
     */
//    private void dismissPlOnUi() {
//        MyPost.post(new Runnable() {
//            @Override
//            public void run() {
//                pl.setVisibility(View.GONE);
//            }
//        });
//    }

    public void showProgress(boolean isShow){
        pl.setVisibility(isShow?View.VISIBLE:View.GONE);
    }

//    private boolean toggleFailed = false;

    /**
     * @param mode
     * @param temp
     * @param geoStatus
     * @param command   操作溫度、模式、开关、出发控制开关
     */
    private void remoteControl(final String mode, final int temp, final int geoStatus, final int command, final View... view) {
        try {
            showProgress(true);
            final GetSmartDeviceResponse deviceResponse = deviceList.get(curPos);
            ControlSmartReq req = getControlSmartReq(mode, temp, geoStatus, deviceResponse, command);
            req.platform = Constants.SMART_MIDEA;
            RequestEntity entity = NetConnectUtil.getAuthRequestEntity(mContext);
            entity.setBody(new Gson().toJson(req));
            service.controlSmartDevice(entity).subscribeOn(mContext.getScheduler().backgroundThread()).observeOn(mContext.getScheduler().mainThread())
                    .subscribe(new Action1<ControlSmartDeviceResponse>() {
                        @Override
                        public void call(ControlSmartDeviceResponse response) {
                            showProgress(false);
                            boolean sucResp = response != null;
                            if (!sucResp)
                                return;
                            if (!TextUtils.isEmpty(mode)) {
                                modeInit((Button) view[0]);
                                return;
                            }
                            if (geoStatus != -1) {
                                deviceResponse.geoFencingStatus = geoStatus;
                                return;
                            }
                            if (temp >= MIN_TEMP) {
                                deviceResponse.profiles.put(ControlSmartReq.IReqKey.REQ_TEMPERATURE, ((SeekBar) view[0]).getProgress() + MIN_TEMP + "");
                                return;
                            }
                            if (command != -1) {
                                commandPower(response, command, deviceResponse, view);
                                if (response.profiles != null && !response.profiles.isEmpty()) {
                                    deviceResponse.profiles = response.profiles;
                                    notifyDataSetChanged();
                                }
                                return;
                            }

                        }
                    }, new Action1<Throwable>() {
                        @Override
                        public void call(Throwable throwable) {
                            showProgress(false);
                            new ToastView(mContext).showToast(mContext.getString(R.string.smart_program_failed));
                            if (temp >= MIN_TEMP) {
                                String temp = deviceResponse.profiles.get(ControlSmartReq.IReqKey.REQ_TEMPERATURE);
                                if (temp != null) {
                                    int t = Integer.parseInt(temp);
                                    ((SeekBar) view[0]).setProgress(t - MIN_TEMP);
                                }
                            }
                            if (geoStatus != -1) {
//                                toggleFailed = true;
                                if (geoStatus == 0) {
                                    view[0].setSelected(true);
                                } else if (geoStatus == 1) {
                                    view[0].setSelected(false);
                                }
//                                toggleFailed = false;
                                return;
                            }
                            if (command != -1) {
                                if (deviceResponse.switchStatus == 1) {
                                    if (view[0] instanceof ImageButton) {
                                        ((ImageButton) view[0]).setImageResource(R.drawable.smart_open_dev);
                                    }
                                } else {
                                    if (view[0] instanceof ImageButton) {
                                        ((ImageButton) view[0]).setImageResource(R.drawable.smart_close_dev);
                                    }
                                }
                                return;
                            }
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
            showProgress(false);
        }
    }

    private void commandPower(ControlSmartDeviceResponse response, int command, GetSmartDeviceResponse deviceResponse, View[] view) {
        if (response != null && response.profiles != null) {
            if (deviceResponse.switchStatus == 1) {
                deviceResponse.switchStatus = 0;
                if (view[0] instanceof ImageButton) {
                    ((ImageButton) view[0]).setImageResource(R.drawable.smart_close_dev);
                }
            } else {
                deviceResponse.switchStatus = 1;
                if (view[0] instanceof ImageButton) {
                    ((ImageButton) view[0]).setImageResource(R.drawable.smart_open_dev);
                }
            }
        }
    }


    /**
     * @param mode
     * @param temp
     * @param geoStatus
     * @param deviceResponse
     * @param command
     * @return 获取控制设备参数
     */
    private ControlSmartReq getControlSmartReq(String mode, int temp, int geoStatus, GetSmartDeviceResponse deviceResponse, int command) {
        ControlSmartReq req = new ControlSmartReq();
        req.deviceId = deviceResponse.deviceId;
        if (geoStatus == -1) {
            req.geoFencingStatus = deviceResponse.geoFencingStatus + "";
        } else {
            req.geoFencingStatus = geoStatus + "";
        }
        req.deviceType = deviceResponse.deviceType;
        req.switchStatus = deviceResponse.switchStatus;
        req.profiles = new HashMap<>();
        if (mode != null)
            req.profiles.put(ControlSmartReq.IReqKey.REQ_MODE, mode);
        if (temp >= 17 && temp <= 30)
            req.profiles.put(ControlSmartReq.IReqKey.REQ_TEMPERATURE, temp + "");
        if (command != -1) {
            req.profiles.put(ControlSmartReq.IReqKey.REQ_SWITCH, command + "");
        }
        return req;
    }

    private void modeInit(Button btn) {
        normalClick(lastModeClickBtn, btn);
        lastModeClickBtn = btn;
    }

    private void distanceInit(Button btn) {
        normalClick(lastDistanceClickBtn, btn);
        lastDistanceClickBtn = btn;
    }

    /**
     * @param range 保存详情中的电子围栏
     */
    private void saveSmartGeoFencing(String range, final View view, int index) {
        showProgress(true);
        try {
            GetSmartDeviceResponse smartDevice = deviceList.get(curPos);
            String deviceID = smartDevice.deviceId;
            SaveGeoFencingReq req = getSaveGeoFencingReq(range, index);
            RequestEntity entity = NetConnectUtil.getAuthRequestEntity(mContext);
            entity.setBody(new Gson().toJson(req));
            service.saveSmartGeoFencing(deviceID,entity).subscribeOn(mContext.getScheduler().backgroundThread()).observeOn(mContext.getScheduler().mainThread())
                    .subscribe(new Action1<GeoFencingEntity>() {
                        @Override
                        public void call(GeoFencingEntity fencingEntity) {
                           showProgress(false);
                            if (fencingEntity != null && fencingEntity.geoFencingID != null) {
                                distanceInit((Button) view);
                                if (!TextUtils.isEmpty(fencingEntity.range))
                                    geoEntitys.add(fencingEntity);
                            }
                        }
                    }, new Action1<Throwable>() {
                        @Override
                        public void call(Throwable throwable) {
                            showProgress(false);
                            new ToastView(mContext).showToast(mContext.getString(R.string.smart_distance_failed));
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
            showProgress(false);
        }

    }


    /**
     * @param range
     * @return 获取保存电子围栏的参数
     */
    private SaveGeoFencingReq getSaveGeoFencingReq(String range, int index) {
        SaveGeoFencingReq req = new SaveGeoFencingReq();
        try {
            if (geoEntitys != null && !geoEntitys.isEmpty()) {
                for (GeoFencingEntity entity : geoEntitys) {
                    int len = entity.range.lastIndexOf(".");
                    String subRange = entity.range.substring(0,len);
                    String _range = subRange + entity.rangeUnit;
                    if (_range.equalsIgnoreCase(range)) {
                        req.geoFencingID = entity.geoFencingID;
                        break;
                    }
                }
                if (TextUtils.isEmpty(req.geoFencingID)) {
                    setupRange(range, index, req);
                }
            } else {
                setupRange(range, index, req);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return req;
    }

    private void setupRange(String range, int index, SaveGeoFencingReq req) {
        if (index == 0) {
            req.range = range.substring(0, range.length() - 1);
            req.rangeUnit = range.substring(range.length() - 1, range.length()).toUpperCase();
        } else {
            req.range = range.substring(0, range.length() - 2);
            req.rangeUnit = range.substring(range.length() - 2, range.length()).toUpperCase();
        }
    }


    /**
     * @param lastClickBtn
     * @param btn          按钮点击样式变换
     */
    private void normalClick(Button lastClickBtn, Button btn) {
        if (lastClickBtn != null) {
            lastClickBtn.setBackgroundResource(R.drawable.smart_add_dev_btn);
            lastClickBtn.setTextColor(mContext.getResources().getColor(R.color.smart_pg));
        }
        if (btn != null) {
            btn.setTextColor(mContext.getResources().getColor(R.color.white));
            btn.setBackgroundResource(R.drawable.smart_item_btn);
        }
    }

    class ChildHolder {
        RelativeLayout ac_sw_rl;
        RelativeLayout trigger_sw_rl;
        TextView acTitleTv, triggerTitleTv;
        ToggleButton triggerTb;
        Button codeBtn, dryBtn, autoBtn, warmBtn, windBtn;
        Button oneBtn, twoBtn, threeBtn, fourBtn, fiveBtn;
        SeekBar tempSb;
        TextView tempTv, insideTempTv;
        TextView current_temp_tv;
        ImageButton switch_iv;
    }

    /**
     * @param btns 距离按钮设置初始化文字
     */
    private void setDistanceText(Button... btns) {
        if (btns == null)
            return;
        int N = btns.length;
        for (int i = 0; i < N; i++) {
            btns[i].setText(mDistance[i]);
        }
    }


    @Override
    public int getGroupType(int groupPosition) {
        return 1;
    }

    @Override
    public int getGroupTypeCount() {
        return 1;
    }

    @Override
    public ContentViewWrapper getGroupViewAndReUsable(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        boolean reUseable = true;
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.fragment_device_list_item, null);
            holder = new ViewHolder();
            holder.device_iv = (ImageView) convertView.findViewById(R.id.device_iv);
            holder.arrow_iv = (ImageView) convertView.findViewById(R.id.arrow_iv);
            holder.device_name_tv = (TextView) convertView.findViewById(R.id.device_name_tv);
//            holder.temp_tv = (TextView) convertView.findViewById(R.id.temp_tv);
            convertView.setTag(holder);
            reUseable = false;
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        GetSmartDeviceResponse item = (GetSmartDeviceResponse) getGroup(groupPosition);
        holder.device_iv.setImageResource(R.drawable.smart_handing_air_conditioning);
        String deviceName = item.deviceName;
        holder.device_name_tv.setText(TextUtils.isEmpty(deviceName) ? mContext.getString(R.string.smart_default_name): deviceName);
        if (isExpanded) {
            holder.arrow_iv.setImageResource(R.drawable.gray_arrow_up);
        } else {
            holder.arrow_iv.setImageResource(R.drawable.gray_arrow_down);
        }
        return new ContentViewWrapper(convertView, reUseable);
    }

    @Override
    public ContentViewWrapper getChildViewAndReUsable(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder holder = null;
        boolean reUseable = true;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.fragment_device_list_sub_item,
                    null);
            holder = new ChildHolder();
            holder.ac_sw_rl = (RelativeLayout) convertView.findViewById(R.id.ac_sw_rl);
            holder.acTitleTv = (TextView) holder.ac_sw_rl.findViewById(R.id.item_title_tv);
            holder.trigger_sw_rl = (RelativeLayout) convertView.findViewById(R.id.trigger_sw_rl);
            holder.triggerTitleTv = (TextView) holder.trigger_sw_rl.findViewById(R.id.item_title_tv);
            holder.triggerTb = (ToggleButton) holder.trigger_sw_rl.findViewById(R.id.switch_tb);
            holder.codeBtn = (Button) convertView.findViewById(R.id.code_btn);
            holder.dryBtn = (Button) convertView.findViewById(R.id.dehumidifier_btn);
            holder.autoBtn = (Button) convertView.findViewById(R.id.auto_btn);
            holder.warmBtn = (Button) convertView.findViewById(R.id.heat_btn);
            holder.windBtn = (Button) convertView.findViewById(R.id.swapping_btn);
            holder.oneBtn = (Button) convertView.findViewById(R.id.one_k_btn);
            holder.twoBtn = (Button) convertView.findViewById(R.id.two_k_btn);
            holder.threeBtn = (Button) convertView.findViewById(R.id.three_k_btn);
            holder.fourBtn = (Button) convertView.findViewById(R.id.four_k_btn);
            holder.fiveBtn = (Button) convertView.findViewById(R.id.five_k_btn);
            holder.tempSb = (SeekBar) convertView.findViewById(R.id.temp_sb);
            holder.tempTv = (TextView) convertView.findViewById(R.id.temp_tv);
            holder.insideTempTv = (TextView) convertView.findViewById(R.id.inside_temp_tv);
            holder.current_temp_tv = (TextView) convertView.findViewById(R.id.current_temp_tv);
            holder.switch_iv = (ImageButton) convertView.findViewById(R.id.switch_iv);
            convertView.setTag(holder);
            reUseable = false;
        } else {
            holder = (ChildHolder) convertView.getTag();
        }
        final GetSmartDeviceResponse item = deviceList.get(groupPosition);
        holder.acTitleTv.setText(R.string.smart_air_sw);
        holder.triggerTitleTv.setText(R.string.smart_trigger_sw);
        holder.oneBtn.setOnClickListener(this);
        holder.twoBtn.setOnClickListener(this);
        holder.threeBtn.setOnClickListener(this);
        holder.fourBtn.setOnClickListener(this);
        holder.fiveBtn.setOnClickListener(this);
        holder.codeBtn.setOnClickListener(this);
        holder.dryBtn.setOnClickListener(this);
        holder.autoBtn.setOnClickListener(this);
        holder.warmBtn.setOnClickListener(this);
        holder.windBtn.setOnClickListener(this);
        setDistanceText(holder.oneBtn, holder.twoBtn, holder.threeBtn, holder.fourBtn, holder.fiveBtn);
        if (lastModeClickBtn == null)
            lastModeClickBtn = holder.codeBtn;
        if (lastDistanceClickBtn == null)
            lastDistanceClickBtn = holder.oneBtn;
        final TextView curTempTv = holder.current_temp_tv;
        initCurrentTv(holder.insideTempTv, item);
        holder.tempSb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int progress = seekBar.getProgress();
                curTempTv.setText((MIN_TEMP + progress) + "℃");
                remoteControl(null, MIN_TEMP + progress, -1, -1, seekBar);
            }
        });

        holder.triggerTb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    int geoFencingStatus = item.geoFencingStatus;
                    if(geoFencingStatus == 1){
                        compoundButton.setSelected(false);
                        remoteControl(null, -1, 0, -1, compoundButton);
                    }else{
                        compoundButton.setSelected(true);
                        remoteControl(null, -1, 1, -1, compoundButton);
                    }
//                    if (geoFencingStatus == 0) {
//                        Object reqMode = item.profiles.get(ControlSmartReq.IReqKey.REQ_MODE);
//                        if (reqMode != null)
//                            remoteControl(null, -1, 1, -1, compoundButton);
//                    } else {
//                        Object reqMode = item.profiles.get(ControlSmartReq.IReqKey.REQ_MODE);
//                        if (reqMode != null)
//                            remoteControl(null, -1, 0, -1, compoundButton);
//                    }
//                }
            }
        });
        if (item.switchStatus == 1) {
            holder.switch_iv.setImageResource(R.drawable.smart_open_dev);
        } else {
            holder.switch_iv.setImageResource(R.drawable.smart_close_dev);
        }
        if (item.geoFencingStatus == 1) {
            holder.triggerTb.setSelected(true);
        } else {
            holder.triggerTb.setSelected(false);
        }
        holder.switch_iv.setOnClickListener(this);
        HashMap<String, String> profiles = item.profiles;
        if (profiles != null && !profiles.isEmpty()) {
            getTemp(curTempTv, holder.tempSb, profiles);
            String reqMode = profiles.get(ControlSmartReq.IReqKey.REQ_MODE);
            if (reqMode != null)
                initModeBtn(reqMode, holder);
        }
        GeoFencingEntity entity = item.geoFencing;
        if (entity != null)
            initTriggerDistance(entity, holder.oneBtn, holder.twoBtn, holder.threeBtn, holder.fourBtn, holder.fiveBtn);
        return new ContentViewWrapper(convertView, reUseable);
    }

    /**
     * @param tv
     * @param res
     * 获取当前室内温度并显示
     */
    private void initCurrentTv(TextView tv, GetSmartDeviceResponse res) {
        if (res == null)
            return;
        HashMap<String,String> profiles = res.profiles;
        if(profiles != null && !profiles.isEmpty()) {
            String reqMode = res.profiles.get(ControlSmartReq.IReqKey.REQ_TEMP_IN);
            String temp = String.format(mContext.getString(R.string.smart_temp_cur), reqMode == null ? "" : reqMode.toString() + "℃");
            tv.setText(temp);
        }
    }

    /**
     * @param curTempTv
     * @param profiles  获取温度，并设置seekbar
     */
    private void getTemp(TextView curTempTv, SeekBar seekBar, HashMap<String, String> profiles) {
        String temp = profiles.get(ControlSmartReq.IReqKey.REQ_TEMPERATURE);
        String temperature = MIN_TEMP + "";
        if (temp != null) {
            temperature = temp;
        }
        curTempTv.setText(temperature + "℃");
        seekBar.setProgress(Integer.parseInt(temperature) - MIN_TEMP);
    }

    /**
     * @param entity
     * @param btns   距离数据初始化
     */
    private void initTriggerDistance(GeoFencingEntity entity, Button... btns) {
        if (entity == null || btns == null)
            return;
        String _range = entity.range.substring(0, entity.range.lastIndexOf("."));
        String range = _range + entity.rangeUnit;
        int N = mDistance.length;
        for (int i = 0; i < N; i++) {
            if (range.equalsIgnoreCase(mDistance[i])) {
                distanceInit(btns[i]);
            }
        }
    }

    /**
     * @param mode
     * @param holder 模式数据初始化
     */
    private void initModeBtn(String mode, ChildHolder holder) {
        if (mode.equalsIgnoreCase(Mode.COLD.getValue())) {
            modeInit(holder.codeBtn);
        } else if (mode.equalsIgnoreCase(Mode.AUTO.getValue())) {
            modeInit(holder.autoBtn);
        } else if (mode.equalsIgnoreCase(Mode.WARM.getValue())) {
            modeInit(holder.warmBtn);
        } else if (mode.equalsIgnoreCase(Mode.DRY.getValue())) {
            modeInit(holder.dryBtn);
        } else if (mode.equalsIgnoreCase(Mode.WIND.getValue())) {
            modeInit(holder.windBtn);
        } else if (mode.equalsIgnoreCase(Mode.NA.getValue())) {
            modeInit(holder.codeBtn);
        }
    }

    public Object getGroup(int groupPosition) {
        return deviceList.get(groupPosition);
    }

    public int getGroupCount() {
        return deviceList.size();
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    class ViewHolder {
        ImageView device_iv;
        ImageView arrow_iv;
        TextView device_name_tv;
//        TextView temp_tv;
    }

    @Override
    public boolean isGroupSwipable(int groupPosition) {
        return true;
    }

    @Override
    public boolean isChildSwipable(int groupPosition, int childPosition) {
        return false;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public boolean hasStableIds() {
        return true;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        super.onGroupExpanded(groupPosition);

    }


    @Override
    public void onGroupCollapsed(int groupPosition) {
        super.onGroupCollapsed(groupPosition);
    }

    //    NA-无效, AUTO-自动, COLD-制冷, DRY-抽湿, WARM-制热, WIND-送风
    enum Mode {
        NA("NA"),
        AUTO("AUTO"),
        COLD("COLD"),
        DRY("DRY"),
        WARM("WARM"),
        WIND("WIND");

        private final String value;

        Mode(String v) {
            value = v;
        }

        public String getValue() {
            return value;
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.one_k_btn:
                saveSmartGeoFencing(mDistance[0], view, 0);
                break;
            case R.id.two_k_btn:
//                distanceInit((Button) view);
                saveSmartGeoFencing(mDistance[1], view, 1);
                break;
            case R.id.three_k_btn:
//                distanceInit((Button) view);
                saveSmartGeoFencing(mDistance[2], view, 2);
                break;
            case R.id.four_k_btn:
//                distanceInit((Button) view);
                saveSmartGeoFencing(mDistance[3], view, 3);
                break;
            case R.id.five_k_btn:
//                distanceInit((Button) view);
                saveSmartGeoFencing(mDistance[4], view, 4);
                break;
            case R.id.code_btn:
                remoteControl(Mode.COLD.getValue(), -1, -1, -1, view);
                break;
            case R.id.dehumidifier_btn:
                remoteControl(Mode.DRY.getValue(), -1, -1, -1, view);
                break;
            case R.id.auto_btn:

                remoteControl(Mode.AUTO.getValue(), -1, -1, -1, view);
                break;
            case R.id.heat_btn:
//                modeInit((Button) view);
                remoteControl(Mode.WARM.getValue(), -1, -1, -1, view);
                break;
            case R.id.swapping_btn:
//                modeInit((Button) view);
                remoteControl(Mode.WIND.getValue(), -1, -1, -1, view);
                break;
            case R.id.switch_iv:
                switchCommandClick((ImageButton) view);
                break;
        }
    }
}


